package com.cn.por.auto.factory;

import com.cn.por.auto.dto.DataModel;
import com.cn.por.auto.dto.ElementUiDataModel;
import com.cn.por.auto.dto.MapperRelation;
import com.cn.por.auto.dto.ModelParam;
import com.cn.por.auto.entity.TableDetiles;
import com.cn.por.auto.enums.ModelType;
import com.cn.por.util.freemarker.StringFormaterUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成elementui框架的基础代码 index add modify preview js五个文件
 */
public class ElementUiFactory implements DataModelGenaFactory{

    private ModelParam modelParam;

    public ElementUiFactory() {
    }

    public ElementUiFactory(ModelParam modelParam) {
        this.modelParam = modelParam;
    }

    @Override
    public DataModel getDataModel(ModelType dataModelType) {
        ElementUiDataModel model = new ElementUiDataModel();
        String meta = StringFormaterUtil.columnToProperty(this.modelParam.isPrefix()? this.modelParam.getTableName().substring(this.modelParam.getTableName().indexOf("_")+1):this.modelParam.getTableName() );
        model.setMeta(meta);
        model.setBaseUrl("/web/"+ this.modelParam.getBasePackage().substring(this.modelParam.getBasePackage().lastIndexOf(".")+1)+"/"+StringFormaterUtil.lowerCaptureName(meta) +"/");
        model.setApiUrl(this.modelParam.getBasePackage().substring(this.modelParam.getBasePackage().lastIndexOf(".")+1)+"/"+StringFormaterUtil.lowerCaptureName(meta));
        model.setTableComment(this.modelParam.getTableComment().replace("表",""));
        model.setPrimaryKeyProperty(StringFormaterUtil.lowerCaptureName(StringFormaterUtil.columnToProperty(this.modelParam.getPrimaryKey())));
        /**
         * 查询条件
         */
        model.setSearchList(this.getRelation(this.modelParam.getConditionColumn()));
        /**
         * 显示字段
         */
        model.setShowProperty(this.getRelation(this.modelParam.getTableColumn()));
        String  fileOrginName =  StringFormaterUtil.lowerCaptureName(meta);
        String dir = this.modelParam.getBasePackage().substring(this.modelParam.getBasePackage().lastIndexOf(".")+1);
        switch (dataModelType){
            case JS:
                model.setZipDir("api/"+dir+"/"+fileOrginName+".js");
                model.setFtlName("elementui/js.ftl");
                break;
            case INDEX:
                model.setZipDir("views/"+dir+"/"+fileOrginName+"/index.vue");
                model.setFtlName("elementui/index.ftl");
                break;
            case ADD:
                model.setZipDir("views/"+dir+"/"+fileOrginName+"/add-dialog.vue");
                model.setFtlName("elementui/add.ftl");
                break;
            case MODIFY:
                model.setZipDir("views/"+dir+"/"+fileOrginName+"/modify-dialog.vue");
                model.setFtlName("elementui/modify.ftl");
                break;
            case PREVIEW:
                model.setZipDir("views/"+dir+"/"+fileOrginName+"/preview-dialog.vue");
                model.setFtlName("elementui/preview.ftl");
                break;
            default:
                break;
        }
        return model;
    }

    private List<MapperRelation> getRelation(List<TableDetiles> tableDetiles){
        List<MapperRelation> showProperty = new ArrayList<>();
        for (TableDetiles item : tableDetiles){
            MapperRelation search = new MapperRelation();
            search.setProperty(StringFormaterUtil.lowerCaptureName(StringFormaterUtil.columnToProperty(item.getColumnName())));
            search.setColumnComment(item.getColumnComment());
            showProperty.add(search);
        }
        return  showProperty;
    }

}
